var everyDay = new Vue({
    el: "#every_day",
    data: {
        content: 'sdklfjdskahf!'
    },
    computed: {
        getContent: function () {
            return this.content
        }
    },
    created: function () {
        //请求数据，给content赋值
        axios({
            methods: 'get',
            url: 'queryEveryDay'
        }).then((res) => {
            console.log(res.data.data[0].content);
            this.content = res.data.data[0].content
            console.log(this.content);
        }).catch((res) => {
            console.log("请求失败")
        })
    }
})

var article_list = new Vue({
    el: "#article_list",
    data: {
        page: 1,
        pageSize: 3,
        count:10,
        pageNumList: [], 
        articleList: [
            {
                title: 'Laravel5.4安装passport时遇到的一些问题',
                content: '安装时可能不支持高版本，我使用了composer require laravel/passport ~4.0安装后执行迁移时nothing to migrate，需要手动注册Provider， 在config/app.php中providers中添加Laravel\Passport\PassportServiceProvider::class。执行php artisan passport:install时提示“There are no commands defined in the “passport” namespace.” 需要执行cache:clear和config:cache 更新缓存。...',
                date: '2019-12-06',
                views:'133',
                tags: 'laravel',
                id:'1',
                link:''
            },
        
    ]
    },
    computed: {
        jumTo: function () {
            return function (page) {
                this.getPage(page, this.pageSize)
            }
        },
        getPage: function () {
            return function (page, pageSize) {

                const searcheUrlParams = location.search.indexOf("?") > -1 ? location.search.split("?")[1].split("&"): "";
                var tag = "";
                for (var i = 0; i < searcheUrlParams.length; i ++) {
                    if (searcheUrlParams[i].split("=")[0] == "tag"){
                        try {
                            tag = searcheUrlParams[i].split("=")[1];
                        }catch (e) {
                            console.log(e)
                        }
                    }
                }

                if(tag == ""){

                    axios({
                        method: 'get',
                        url: "/queryBlog?page=" + (page-1) + "&pageSize=" + pageSize
                    }).then((res) => {
                        const result = res.data.data;
                        const list = [];
                        
                        for(var i = 0; i < result.length; i ++) {
                            var temp = {}; 
                            temp.title = result[i].title;
                            temp.content = result[i].content;
                            temp.views = result[i].views;
                            temp.tags = result[i].tags;
                            temp.data = result[i].ctime;
                            temp.id = result[i].id;
                            temp.link = "/blog_detail.html?bid=" + result[i].id;
                            list.push(temp);
                        }
                        article_list.articleList = list;
                        article_list.page = page;
                    }).catch((res) => {
                         console.log("请求错误")
                    })
    
                    axios({
                        method: "get",
                        url: "/queryBlogCount"
                    }).then((res) => {
                        article_list.count = res.data.data[0].count;
                        this.generatePageTool;
                    })
                }else {
                    axios({
                        method: 'get',
                        url: "/queryByTag?page=" + (page-1) + "&pageSize=" + pageSize + "&tag=" + tag
                    }).then((res) => {
                        console.log(res);
                        const result = res.data.data;
                        const list = [];
                        for(var i = 0; i < result.length; i ++) {
                            var temp = {}; 
                            temp.title = result[i].title;
                            temp.content = result[i].content;
                            temp.views = result[i].views;
                            temp.tags = result[i].tags;
                            temp.data = result[i].ctime;
                            temp.id = result[i].id;
                            temp.link = "/blog_detail.html?bid=" + result[i].id;
                            list.push(temp);
                        }
                        article_list.articleList = list;
                        article_list.page = page;
                    }).catch((res) => {
                         console.log("请求错误")
                    })

                    axios({
                        method: "get",
                        url: "/queryByTagCount?tag=" + tag
                    }).then((res) => {
                        article_list.count = res.data.data[0].count;
                        this.generatePageTool;
                    })
                }
            }
        },

        generatePageTool: function () {
            const nowPage = this.page;
            const pageSize = this.pageSize;
            const totalCount = this.count;
            const result = [];
            result.push({text: "<<", page: 1});
            if (nowPage > 2) {
            result.push({tex: nowPage - 2, page: nowPage - 2})
            };
            if (nowPage > 1) {
                result.push({text: nowPage - 1, page:nowPage - 1})
            }
            result.push({text:nowPage, page: nowPage});
            if (nowPage + 1 <= (totalCount + pageSize - 1) / pageSize) {
                result.push({text: nowPage + 1, page: nowPage + 1});
            }
            if (nowPage + 2 <= (totalCount + pageSize - 1) / pageSize) {
                result.push({text: nowPage + 2, page: nowPage + 2});
            }
            result.push({text: ">>", page: parseInt((totalCount + pageSize - 1) / pageSize)});
            this.pageNumList = result;
            return result;
        }
    },
    created: function () {
        this.getPage(this.page, this.pageSize)
    }
})